package com.example.resourceSharingPlatform.security_service.impl;

import java.util.List;

import com.example.resourceSharingPlatform.dao.AccountDao;
import com.example.resourceSharingPlatform.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.example.resourceSharingPlatform.dao.UserDao;
import com.example.resourceSharingPlatform.entity.User;
import com.example.resourceSharingPlatform.security_dao.UsersDao;
import com.example.resourceSharingPlatform.security_entity.Users;
import com.example.resourceSharingPlatform.security_service.UsersService;
/**
 * UserService实体类
 * 1.selectUserByUserName(String userName)
 *   通过用户名在数据库查询用户信息
 * 2.insertUsers(Users users)
 *   在users表中插入一条数据
 *   在user表中插入一条数据
 *   要求：
 *   users.user_name=user.id
 */
@Service
public class UsersServiceImpl implements UsersService {

	//修改users表
    @Autowired
    private UsersDao usersDao;
    
    //修改user表
    @Autowired
    private UserDao userDao;

    //修改Account表
    @Autowired
    private AccountDao accountDao;

    @Override
    public Users selectUserByUserName(String userName) {
        //Users user = new Users();
        //user.setUserName(userName);
        List<Users> list = usersDao.queryUsersByUserName(userName);
        return list.isEmpty() ? null : list.get(0);
    }

    
	/*
	 * 收到注册请求后向users和user表中插入数据
	 * 
	 * @Parameter users
	 * 传入后只有user_name和password有参数
	 * password为明文密码
	 * 
	 *要同时修改users表和user表
	 */
    @Override
    public Boolean insertUsers(Users users){
        //如果数据库中没有数据则注册
        if(selectUserByUserName(users.getUsername())==null)
        {
            //初始化Users实例
            Users newUsers = initializeUsers(users);
            //初始化User实例
            User user = initializeUser(newUsers);
            //初始化Account
            Account account = initializeAccount(users.getUserName());
            //把表的信息存储到数据库中
            Users currentUsers = usersDao.insertUsers(newUsers);
            userDao.insertUser(user);
            accountDao.insertAccount(account);


            return selectUserByUserName(currentUsers.getUsername())!=null;
        }
        else return false;


    }



    /*
     * 初始化Users实例的函数
     */
    private Users initializeUsers(Users users) {
    	
    	//首先要进行密码加密，采用BCrypt加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword =  encoder.encode(users.getPassword());
        
        /*
         * 为users填充默认值
         * @Attribute userId Long
         * @Attribute userName String
         * @Attribute password String
         * @Attribute status String
	     * @Attribute roles String
	     * @Attribute authorities List<GrantedAuthority>
         */
        Users newUsers = new Users();
        //userId         为自增不需要填充
        //userName       已经传入了
        newUsers.setUserName(users.getUserName());
        //password       填充为加密后的字符串
        newUsers.setPassword(encodedPassword);
        //status         默认为可用状态0
        newUsers.setStatus("0");
        //roles          通过用户注册页面注册的用户默认为用户权限“ROLE_USER”
        newUsers.setRoles("ROLE_USER");
        //authorities    这个属性不需要存储到数据库中
        
        return newUsers;
    }
    
    
    /*
     * 初始化User实例的函数
     */
    private User initializeUser(Users users) {
        /*
         * 为user填写默认值
         * @Attribute id      String
	     * @Attribute name    String
	     * @Attribute photo   String
	     * @Attribute gender  String
	     * @Attribute age     int
	     * @Attribute tel     String 
         */
        User user = new User();
        //id
        user.setId(users.getUserName());
        //name
        user.setName("我是一棵小虎牙");
        //photo
        user.setPhoto("F:\\Grade_three\\程序设计高级实践\\Try\\vue_shop\\src\\assets\\img\\default.jpg");
        //gender
        user.setGender("unknown");
        //age
        user.setAge(0);
        //tel
        user.setTel("unknown");
        
        return user;    	
    }

    /*
    *  初始化Account
    * */
    private Account initializeAccount(String id){
        Account account =new Account();
        account.setId(id);
        account.setBalance(0.0);
        account.setCredit(100);
        return account;
    }
}
